package com.lw.main;

import com.lw.main.word.GameRecord;
import com.lw.main.word.GameTime;
import com.lw.util.Constant;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;

/**
 * 游戏主窗口
 */
public class GameFrame extends Frame {

    //解决屏幕闪烁
    private BufferedImage buffImg = new BufferedImage(Constant.WIDTH,Constant.HEIGHT,BufferedImage.TYPE_4BYTE_ABGR);

    private GameBackGround gameBackGround;//背景

    private GameFrontGround frontGround;//前景

    private Bird bird;//鸟

    private GameBarrierLayer barrierLayer;//障碍物

    private GameTime gameTime;//计时器
    private GameRecord gameRecord;//分数记录

    public GameFrame() {
        setVisible(true);//设置窗口可见
        setSize(Constant.WIDTH,Constant.HEIGHT);//窗口大小
        setTitle(Constant.Title);
        setLocation(Constant.FRAME_X,Constant.FRAME_Y);//窗口位置
        setResizable(false);//窗口大小不可改变

        //设置窗口关闭，程序退出
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                gameRecord.saveRecord();

                System.exit(0);
            }
        });

        initGame();

        new Run().start();

        //添加按键监听
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                add(e);
            }

            @Override
            public void keyReleased(KeyEvent e) {
                minu(e);
            }
        });
    }

    public void add(KeyEvent e){
        switch (e.getKeyCode()){
            case KeyEvent.VK_UP -> bird.fly(1);
            case KeyEvent.VK_SPACE -> {
                if(!bird.isLife()){
                    restart();
                }
            }
        }
    }

    public void minu(KeyEvent e){
        switch (e.getKeyCode()){
            case KeyEvent.VK_UP -> bird.fly(5);
        }
    }

    public void restart(){
        barrierLayer.clearBarriers();
        bird.restartDraw();
        gameTime.begin();
    }

    public void initGame(){
        gameBackGround = new GameBackGround();
        frontGround = new GameFrontGround();
        barrierLayer = new GameBarrierLayer();
        bird = new Bird();

        gameRecord = new GameRecord();
        gameTime = new GameTime();
        gameTime.begin();
    }

    @Override
    public void update(Graphics g) {
        if(!bird.isLife()){//小鸟死亡
            this.gameRecord.saveRecord();

            String over = "游戏结束";
            g.setColor(Color.red);
            g.setFont(new Font("微软雅黑",Font.BOLD,60));
            g.drawString(over,Constant.WIDTH/2-60*over.length()/2,Constant.HEIGHT/2);

            String reset = "按下空格重新开始游戏";
            g.setFont(new Font("微软雅黑",Font.PLAIN,30));
            g.drawString(reset,Constant.WIDTH/2-30*reset.length()/2,Constant.HEIGHT/2+60);

            return;
        }

        Graphics imgG = buffImg.getGraphics();

        //将内容绘制到图片上 注意后绘制的会覆盖先绘制的
        gameBackGround.draw(imgG);
        frontGround.draw(imgG);
        barrierLayer.draw(imgG,bird);
        bird.draw(imgG);

        gameTime.draw(imgG);
        gameRecord.draw(imgG,gameTime.getDiffer());

        //将图片绘制到屏幕上，解决屏幕闪烁问题
        g.drawImage(buffImg,0,0,null);
    }

    class Run extends Thread{
        @Override
        public void run() {
            while (true){
                repaint();
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
